/// ./lin/utils/db_util.dart
import 'dart:io';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart';

/// Hive 数据操作
class DBUtil {
  /// 实例
  static DBUtil? instance;

  /// 标签
  Box? tagsBox;

  /// 初始化，需要在 main.dart 调用
  /// <https://docs.hivedb.dev/>
  static Future<void> install() async {
    /// 初始化数据库地址
    Directory document = await getApplicationDocumentsDirectory();
    Hive.init(document.path);
    /// 注册自定义对象（实体）
    /// <https://docs.hivedb.dev/#/custom-objects/type_adapters>
    /// Hive.registerAdapter(SettingsAdapter());
  }

  /// 初始化 Box
  static Future<DBUtil?> getInstance() async {
    if (instance == null) {
      instance = DBUtil();
      await Hive.initFlutter();

      /// 消息盒子
      instance!.tagsBox = await Hive.openBox('messageBox');

      /// 好友申请盒子
      instance!.tagsBox = await Hive.openBox('friendApplyBox');
    }
    return instance;
  }
}